package com.gyh.stlserver.controller;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.gyh.stlserver.constants.Constants;
import com.gyh.stlserver.entity.Member;
import com.gyh.stlserver.entity.Reserve;
import com.gyh.stlserver.service.MemberService;
import com.gyh.stlserver.service.ReserveService;
import com.gyh.stlserver.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/member")
public class MemberController {
    @Autowired
    private MemberService memberService;
    @Autowired
    private ReserveService reserveService;
    @GetMapping
    public Result search(@RequestParam(defaultValue = "1") Integer pageNum,
                         @RequestParam(defaultValue = "10") Integer pageSize, Member member){
        return Result.success(Constants.STATUS_200,memberService.getAllMember(pageNum,pageSize,member));
    }
    @GetMapping("/getAll")
    public Result getAll(){
        return Result.success(Constants.STATUS_200,memberService.getAll());
    }
    /*添加会员*/
    @PostMapping
    public Result addMember(@RequestBody Member member){
        int i = memberService.addMember(member);
        if (i<1) {
            return Result.error(Constants.STATUS_401,"添加失败");
        }
        return Result.success(Constants.STATUS_200);
    }
    /*校验名称*/
    @GetMapping("/checkName")
    public Result checkName(String name){
        Member member = memberService.checkName(name);
        if (ObjectUtil.isNotEmpty(member)) {
            return Result.error(Constants.STATUS_403,"名称已存在，请重新填写名称");
        }
        return Result.success(Constants.STATUS_200);
    }
    /*校验手机号*/
    @GetMapping("/checkPhone")
    public Result checkPhone(String phone){
        Member member = memberService.checkPhone(phone);
        if (ObjectUtil.isNotEmpty(member)) {
            return Result.error(Constants.STATUS_402,"手机号已存在，请重新填写名称");
        }
        return Result.success(Constants.STATUS_200);
    }
    @PostMapping("/editMember")
    public Result editMember(@RequestBody Member member){
        /*
        * 验证手机号
        * 1.当手机号为当前手机号通过
        * 2.当手机号不是自己的手机号，但是其他用户的手机号不通过
        * */
        /*先验证用户是否存在*/
        Member dbMember = memberService.getMemberById(member.getId());
        if (ObjectUtil.isEmpty(dbMember)){
            return Result.error(Constants.STATUS_405,"会员不存在，无法删除");
        }
        Member checkPhone = memberService.checkPhone(member.getPhone());
        if (!dbMember.getPhone().equals(member.getPhone())&&ObjectUtil.isNotEmpty(checkPhone)){
            return Result.error(Constants.STATUS_402,"手机号存在");
        }
        int i =  memberService.editMember(member);
        if (i<1) {
            return Result.error(Constants.STATUS_401,"编辑失败");
        }
        return Result.success(Constants.STATUS_200);
    }
    @GetMapping("/remove")
    public Result remove(Integer id){
        Member dbMember = memberService.getMemberById(id);
        if (ObjectUtil.isEmpty(dbMember)){
            return Result.error(Constants.STATUS_405,"会员不存在");
        }
        /*删除前查看该会员是否有未完成的课程*/
        List<Reserve> reserves = reserveService.getReserveByMid(id);
        if (ObjectUtil.isNotEmpty(reserves)){
            return Result.error(Constants.STATUS_406,"会员还有未完成的课程，不能删除");
        }
        int i = memberService.remove(id);
        if (i<1){
            return Result.error(Constants.STATUS_401,"删除会员失败");
        }
        return Result.success(Constants.STATUS_200);
    }
    /*批量删除*/
    @PostMapping("/delSelected")
    public Result delSelected(@RequestBody Integer[] ids){
        for (Integer id : ids) {
            List<Reserve> reserves = reserveService.getReserveByMid(id);
            if (ObjectUtil.isNotEmpty(reserves)){
                return Result.error(Constants.STATUS_406,"有会员还有未完成的课程，不能删除");
            }
        }
        int i = memberService.removeByIds(ids);
        if (i<1){
            return Result.error(Constants.STATUS_401,"批量删除失败");
        }
        return Result.success(Constants.STATUS_200);
    }
    /*充值*/
    @GetMapping("/pay")
    public Result pay(Double balance,Integer memberId){
        int i = memberService.payBalance(balance,memberId);
        if (i<1){
            return Result.error(Constants.STATUS_401,"充值异常");
        }
        return Result.success(Constants.STATUS_200);
    }
    /*消费*/
    @GetMapping("/consume")
    public Result consume(Double balance,Integer memberId){
        try {
            int i = memberService.consumeBalance(balance,memberId);
            if (i<1){
                return Result.error(Constants.STATUS_401,"充值异常");
            }
        }catch (Exception e){
            return Result.error(Constants.STATUS_401,e.getMessage());
        }

        return Result.success(Constants.STATUS_200);
    }

}
